package com.glsc.ngateway.platform.controller.it.networkassets;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.MaintenanceInformationDto;
import com.glsc.ngateway.platform.service.it.networkAssets.MaintenanceInformationService;
import com.glsc.ngateway.platform.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.platform.utils.report.ReportUploadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/it/networkAssets/maintenanceInformation")
@Api(value = "网络资产-维保信息维护", tags = {"网络资产"})
public class MaintenanceInformationController {
    private static final Logger logger = LoggerFactory.getLogger(MaintenanceInformationController.class);

    @Autowired
    private MaintenanceInformationService mtInfoService;

    /**
     * 查询
     */
    @PostMapping("/search")
    @ApiOperation(value = "查询维保信息维护", notes = "查询维保信息维护")
    public PlatformResponse<Page<MaintenanceInformationDto>> query(@RequestBody PageQueryParamDto<MaintenanceInformationDto> queryParam) throws Exception {
        return PlatformResponse.successData(mtInfoService.query(queryParam));
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除维保信息维护", notes = "删除维保信息维护")
    public PlatformResponse<Void> delete(@RequestBody MaintenanceInformationDto param) {
        mtInfoService.delete(param.getId());
        return PlatformResponse.successMsg("删除成功");
    }

    /**
     * 添加或修改
     */
    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加或修改维保信息维护", notes = "添加或修改维保信息维护")
    public PlatformResponse<Void> addOrUpdate(@RequestBody MaintenanceInformationDto param) {
        mtInfoService.addOrUpdate(param);
        return PlatformResponse.successMsg("添加成功");
    }

    /**
     * 导出数据
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出报表", notes = "导出报表")
    public byte[] export(@RequestBody MaintenanceInformationDto param, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;" + "filename=" + URLEncoder.encode(ReportFileConfigEnum.MAINTENANCE_INFORMATION.getFileName(), "UTF-8"));
        return mtInfoService.export(param);
    }

    /**
     * 导入编辑
     */
    @PostMapping("/upload")
    @ApiOperation(value = "导入编辑", notes = "导入编辑")
    public PlatformResponse<?> upload(@RequestPart("file") MultipartFile file) throws IOException {
        ReportUploadContext<MaintenanceInformationDto, String> context =  mtInfoService.upload(file);
        return context.isCheckPass() ? PlatformResponse.success() : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }
}
